/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.seppiko.chart.services;

import com.google.zxing.oned.Code128Writer;
import com.google.zxing.oned.Code39Writer;
import com.google.zxing.oned.Code93Writer;
import com.google.zxing.oned.ITFWriter;
import org.seppiko.chart.exceptions.SeppikoCheckException;
import org.seppiko.chart.exceptions.SeppikoProcessorException;
import org.seppiko.chart.models.BarcodeEntity;
import org.seppiko.chart.models.ControllerEntity;
import org.seppiko.chart.models.MessageEntity;
import org.seppiko.chart.utils.ImageUtil;
import org.seppiko.chart.utils.JsonUtil;
import org.seppiko.chart.utils.StringUtils;
import org.seppiko.chart.utils.ZxingImageUtil;
import org.seppiko.chart.utils.logging.LoggingManager;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

/**
 * Linear barcodes Generator Service
 *
 * @author Leonard Woo
 */
@Service
public class OneDimensionalService {

  private static final LoggingManager Logger = LoggingManager.getInstance().getLogger(OneDimensionalService.class.getName());

  public ControllerEntity code39CHX(BarcodeEntity entity) {
    return onedCHX(entity, new Code39Writer().encode(entity.getData()));
  }

  public ControllerEntity code93CHX(BarcodeEntity entity) {
    return onedCHX(entity, new Code93Writer().encode(entity.getData()));
  }

  public ControllerEntity code128CHX(BarcodeEntity entity) {
    return onedCHX(entity, new Code128Writer().encode(entity.getData()));
  }

  public ControllerEntity itfCHX(BarcodeEntity entity) {
    return onedCHX(entity, new ITFWriter().encode(entity.getData()));
  }

  private ControllerEntity onedCHX(BarcodeEntity entity, boolean[] matrix) {
    ControllerEntity resp = new ControllerEntity();
    ImageUtil imageUtil = new ImageUtil();
    if (StringUtils.isEmpty(entity.getColor())) {
      entity.setColor(ZxingImageUtil.BLACK + "");
    }
    if (StringUtils.isEmpty(entity.getBackgroundColor())) {
      entity.setBackgroundColor(ZxingImageUtil.WHITE + "");
    }
    try {
      resp.setStatus(200);
      if (entity.getFormat().equalsIgnoreCase("svg")) {
        resp.setMediaType(MediaType.parseMediaType("image/svg+xml"));
        resp.setData(imageUtil.svgGenerator(entity, matrix));
      } else {
        resp.setData(imageUtil.genImage(entity, matrix));
        resp.setMediaType(imageUtil.getType());
      }
    } catch (SeppikoCheckException ex) {
      Logger.error(ex.getMessage(), ex);
      resp.setMediaType(MediaType.APPLICATION_JSON);
      resp.setStatus(501);
      resp.setData(JsonUtil.toJson(new MessageEntity(501, ex.getMessage())).getBytes());
    } catch (SeppikoProcessorException ex) {
      Logger.error("Create failed", ex);
      resp.setMediaType(MediaType.APPLICATION_JSON);
      resp.setStatus(500);
      resp.setData(JsonUtil.toJson(new MessageEntity(500, "Create failed")).getBytes());
    }
    return resp;
  }
}
